package codechicken.wirelessredstone.core;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;

import net.minecraft.server.MinecraftServer;
import net.minecraft.src.*;

public class CoreProxy
{
	public static void HandlePacket(PacketRedstoneEther packet, NetHandler nethandler, IWirelessPacketHandler handler)
	{
		try
        {
            EntityPlayer player = (EntityPlayer)ModLoader.getPrivateValue(net.minecraft.src.NetServerHandler.class, nethandler, 4);
    		handler.handlePacket(player.worldObj, player, packet);
        }
        catch(NoSuchFieldException nosuchfieldexception)
        {
            nosuchfieldexception.printStackTrace();
        }
	}

	public static boolean isClient()
	{
		return false;
	}

	public static void openTileWirelessGui(EntityPlayer entityplayer, ITileWireless tileentity)
	{
	}

	public static void openItemWirelessGui(EntityPlayer entityplayer)
	{
	}
	
	private static MinecraftServer mc = ModLoader.getMinecraftServerInstance();

	public static World getWorld()
	{
		return mc.worldMngr[0];
	}

	public static File getMinecraftDir()
	{
		return new File(".");
	}

	public static File getWorldDirectory(World world, int dimension)
	{
		String subdimension = "";
		if(dimension != 0)
		{
			subdimension = File.separator + "DIM" + dimension;
		}
		return new File(getWorldName() + subdimension);
	}

	private static String getWorldName()
	{
		return mc.propertyManagerObj.getStringProperty("level-name", "world");
	}

	public static EntityPlayer getPlayer()
	{
		return null;
	}

	public static EntityPlayer getPlayer(String playername)
	{
		return mc.configManager.getPlayerEntity(playername);
	}
	
	@SuppressWarnings("unchecked")
	public static ArrayList<EntityPlayer> getAllPlayers()
	{
		return (ArrayList<EntityPlayer>) ((ArrayList<EntityPlayer>) mc.configManager.playerEntities).clone();
	}

	public static ArrayList<EntityPlayer> getPlayersInDimension(int dimension)
	{
		ArrayList<EntityPlayer> allplayers = getAllPlayers();
		for(Iterator<EntityPlayer> iterator = allplayers.iterator(); iterator.hasNext();)
		{
			if(iterator.next().dimension != dimension)
			{
				iterator.remove();
			}
		}
		return allplayers;
	}
	
	public static void sendPacketTo(EntityPlayer player, Packet packet)
	{
		if(player == null)
		{
			for(int i = 0; i < mc.configManager.playerEntities.size(); i++)
            {
                ((EntityPlayerMP)mc.configManager.playerEntities.get(i)).playerNetServerHandler.sendPacket(packet);
            }
		}
		else
		{
			((EntityPlayerMP)player).playerNetServerHandler.sendPacket(packet);
		}
	}

	public static void sendPacketToPlayersWithLoadedChunk(int chunkx, int chunkz, PacketRedstoneEther packet)
	{
		ChunkCoordIntPair chunkloc = new ChunkCoordIntPair(chunkx, chunkz);
		for(int i = 0; i < mc.configManager.playerEntities.size(); i++)
        {
			EntityPlayerMP player = (EntityPlayerMP)mc.configManager.playerEntities.get(i);
			if(player.field_420_ah.contains(chunkloc))
			{
				player.playerNetServerHandler.sendPacket(packet);
			}
        }
	}

	public static int getRemoteIcon(int damage)
	{
		return 0;
	}

	public static String getLocalizedName(String localname, String fullname)
	{
		return null;
	}

	public static boolean isSMP()
	{
		return true;
	}

	public static void sendPacket(Packet descriptionPacket)
	{
	}

	public static World getWorld(int dimension)
	{
		return mc.worldMngr[dimension];
	}

	public static int getArrayDimension(World world)
	{
		if(world.worldProvider.isHellWorld)
		{
			return 1;
		}
		else
		{
			return 0;
		}
	}
	
	public static int getActualDimension(World world)
	{
		if(world.worldProvider.isHellWorld)
		{
			return -1;
		}
		else
		{
			return 0;
		}
	}
	
	public static int getArrayDimension(int dimension)
	{
		if(dimension == -1)
		{
			return 1;
		}
		return 0;
	}

	public static int getActualDimension(int dimension)
	{
		if(dimension == 1)
		{
			return -1;
		}
		return 0;
	}
	
	public static void AddLocalization(String s, String s1)
    {
        Properties properties = null;
        try
        {
            properties = (Properties)ModLoader.getPrivateValue(net.minecraft.src.StringTranslate.class, StringTranslate.getInstance(), 1);
        }
        catch(SecurityException securityexception)
        {
            ModLoader.ThrowException("Wireless Redstone Localization", securityexception);
        }
        catch(NoSuchFieldException nosuchfieldexception)
        {
            ModLoader.ThrowException("Wireless Redstone Localization", nosuchfieldexception);
        }
        if(properties != null)
        {
            properties.put(s, s1);
        }
    }
}
